var util = require('../../../utils/util.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //消费记录列表数据
    expenseData: [],
    searchPageNum: 1,   // 设置加载的第几次，默认是第一次
    RecordStatus:false,  //暂无数据状态
    upslideLoading: true,  //"上拉加载"的变量，默认true，隐藏
    upslideNoData: true,	//“没有数据”的变量，默认true，隐藏
    ifRefresh: false,	//是否是下拉刷新
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
	  app.publicchangenavigate()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //请求
    this.getExpenseList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    wx.showNavigationBarLoading() //在标题栏中显示加载
    //初始化参数
    that.setData({
      searchPageNum: 1,   // 设置加载的第几次，默认是第一次
      upslideLoading: true,  //"上拉加载"的变量，默认true，隐藏
      upslideNoData: true,	//“没有数据”的变量，默认true，隐藏 
      ifRefresh: true,
    })
    //模拟加载
    setTimeout(function () {
      //请求
      that.getExpenseList()
    }, 1000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    if (that.data.upslideNoData && that.data.RecordStatus == false) {
      that.setData({
        searchPageNum: that.data.searchPageNum + 1,//增加分页数
      })
      //请求
      that.getExpenseList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
	return app.globalshare();
  },
  /**
   * 获取消费记录
   */
  getExpenseList:function(){
    var that = this
    var uid = app.globalData.userInfoId
    //开启加载
    if (that.data.ifRefresh == false) {
    that.setData({ upslideLoading: false })
    }
    //提交给服务端注册信息
    util.AJAX('Home/Index/getExpenseList', { uid: uid, page: that.data.searchPageNum }, function (res) { //方法体
      //关闭加载
      if (that.data.ifRefresh == false) {
      that.setData({ upslideLoading: true })
      }

      if (res.data.StatusCode == 100) {  //成功
        if (res.data.Info.length){
          if (that.data.searchPageNum == 1) {
            that.setData({ expenseData: res.data.Info })
          } else {
            var expenseData = that.data.expenseData
            var cs = expenseData.concat(res.data.Info)
            that.setData({
              expenseData: cs,
            })
          }
        }else{
          //更新显示状态和减掉分页
          if (that.data.searchPageNum > 1) {
            that.setData({
              searchPageNum: that.data.searchPageNum - 1,//减掉分页数
              upslideLoading: true,  //"上拉加载"的变量，默认true，隐藏
              upslideNoData: false	//“没有数据”的变量，默认true，隐藏
            })
          }
        }
      } else {	//失败
        //更新显示状态和减掉分页
        if (that.data.searchPageNum > 1) {
          that.setData({
            searchPageNum: that.data.searchPageNum - 1,//减掉分页数
            upslideLoading: true,  //"上拉加载"的变量，默认true，隐藏
            upslideNoData: false	//“没有数据”的变量，默认true，隐藏
          })
        }
      }

      if (that.data.expenseData.length) {
        that.setData({
          RecordStatus: false
        })
      }else{
        that.setData({
          RecordStatus: true
        })
      }

      //判断并关闭下拉刷新
      if (that.data.ifRefresh) {
        that.setData({ ifRefresh: false })
        // complete
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    }, 'POST')
  }
})